<?php

class API_ImageWriter {

    protected $hImage,
              $hColor,
              $ttfFont,
              $ttfFontSize,
              $ttfFontBold,
              $jpegQuality = 90;
    
    public function Open($imagePath){
        $imagePath = DATA_PATH . $imagePath;
        if (!is_file($imagePath) || !list(,,$type) = @getimagesize($imagePath)) return false;
            
        switch ($type)
        {      
          case 1:  $this->hImage = @imagecreatefromgif($imagePath);  break;
          case 2:  $this->hImage = @imagecreatefromjpeg($imagePath);  break;
          case 3:  $this->hImage = @imagecreatefrompng($imagePath);  break;        
          default: $this->hImage = false;
        }
        return $this->hImage?true:false;
    }
    
    public function SetColor($color, $alpha = false){
        if (!$this->hImage) return false;
    
        list($r, $g, $b) = array_map('hexdec', str_split(ltrim($color, '#'), 2));
    
        return $alpha === false ?
            $this->hColor = imagecolorallocate($this->hImage, $r+1, $g+1, $b+1) :
            $this->hColor = imagecolorallocatealpha($this->hImage, $r+1, $g+1, $b+1, $alpha);
    }
    
    public function SetFont($font, $bold = false, $size = 14, $color = false, $alpha = false)
    {                            
        if (!is_file(DATA_PATH . $font . ($bold?'b':'') . '.ttf'))
            return false;

        $this->ttfFont     = $font;     
        $this->ttfFontSize   = $size;
        $this->ttfFontBold   = $bold;

        if ($color) $this->SetColor($color, $alpha);
    }
    
    public function WriteText ($x, $y, $text, $angle = 0){
        if (!$this->ttfFont || !$this->hImage || !$this->hColor) return false;

        imagettftext($this->hImage,
                     $this->ttfFontSize, $angle, $x, $y + $this->ttfFontSize,
                     $this->hColor, DATA_PATH . $this->ttfFont . ($this->ttfFontBold?'b':'') . '.ttf', $text);  
    }
    
    public function WriteArrItem($text, $how, $th = 0){
        if(!isset($how['p'])){                         
            foreach($how as $to)
                $this->WriteArrItem($text, $to, $th);
            return;
        }
        $this->SetFont($this->ttfFont, isset($how['b'])?$how['b']:false, $how['s']);
        $this->WriteText($how['p'][0], $how['p'][1]+$th, $text);        
    }
    
    public function WriteArray($data, $to){
        if(is_array($data)){       
            foreach($data as $k=>$v){  
                if(!isset($to[$k]) || !is_array($to[$k])) continue;
                $cto = $to[$k];                           
                if(is_array($v)){
                    $i = 0;
                    $th = 0;                                       
                    if(isset($cto['_']))
                        $th = $cto['_'];
                    foreach($v as $ki=>$vi){                       
                        $vi['_'] = $th*$i++;      
                        $this->WriteArray($vi, $cto);
                    }
                } else {                                           
                    $this->WriteArrItem($v, $cto, isset($data['_'])?$data['_']:0);  
                }
            }
        }
    }
    
    public function Out($target, $todisk = false){
        //if(is_file ($target) && !$replace) return false;

        if($todisk) 
            $ext = strtolower(substr($target, strrpos($target, ".") + 1));    
        else
            $ext = $target;

        switch($ext)
        {
            case "gif":        
                if($todisk)
                    imagegif($this->hImage, $target);
                else
                    imagegif($this->hImage);                    
                break;
                    
            case "jpg" :
            case "jpeg":
                if($todisk)
                    imagejpeg($this->hImage, $target, $this->jpegQuality);        
                else
                    imagejpeg($this->hImage, NULL, $this->jpegQuality);                            
                break;

            case "png":
                if($todisk)
                    imagepng($this->hImage, $target);
                else
                    imagepng($this->hImage);                    
                break;

            default: return false;
        }
        return true;     
    }
    
    public function __destruct(){
        if ($this->hImage) imagedestroy($this->hImage);
    }
}